-- UIAccountActivate
-- Created by chengb Mar/18/2016
-- 账号激活界面

UIAccountActivate = class("UIAccountActivate", function()
    return cc.Layer:create();
end);

function UIAccountActivate.create(account, channel)
    return UIAccountActivate.new(account, channel);
end

-- 构造函数
function UIAccountActivate:ctor(account, channel)
    self:setName("UIAccountActivate");
    local node = cc.CSLoader:createNode("layout/login/AccountActivate.csb");
    self:addChild(node);
    self.node = node;

    self.account = account;
    self.channel = channel;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIAccountActivate:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIAccountActivate:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIAccountActivate 界面析构清理");
            EventMgr.removeAll("UIAccountActivate");
        end
    end);
end

-- 注册点击事件
function UIAccountActivate:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIAccountActivate");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册领码按钮点击事件
    local btnGet = findChildByName(self.node, "CT/btn_get");
    local function onGetClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            local url = getActivateCodeUrl(self.channel);
            openUrl(url);
        end
    end
    btnGet:addTouchEventListener(onGetClick);
    btnGet:setTitleText(getLocStr("btn_text_get_code"));
    TextStyleM.setTextStyle(btnGet, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);

    -- 注册激活按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            self:verifyCode();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    btnOk:setTitleText(getLocStr("btn_text_activate"));
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
end

-- 认证激活码
function UIAccountActivate:verifyCode()
    -- 检查输入是否合法
    local code = self.editCode:getText();
    code = string.trim(code);

    local len = string.len(code);
    if len <= 0 then
        alert(getLocStr("please_input_activate_code"));
        return;
    end

    local channel = self.channel;
    local loginInfo = get3rdAccountLoginInfo(channel);
    if not loginInfo then
        trace("UIAccountActivate", "未登录第三方账号，无法认证激活码");
        return;
    end

    -- 请求服务器认证激活码
    local account = self.account or loginInfo.account;
    Operation.cmd_activate_account(account, channel, code);
end

-- 重绘
function UIAccountActivate:redraw()
    -- 标题
    local titleLabel = findChildByName(self.node, "CT/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(getLocStr("input_activate_code"));

    local inputLabel = findChildByName(self.node, "CT/input_label");
    TextStyleM.setTextStyle(inputLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    inputLabel:setString(getLocStr("account_activate_input"));

    local codeInput = findChildByName(self.node, "CT/code");
    local editBoxSize = codeInput:getContentSize();

    -- 激活码输入框
    local editCode = ccui.EditBox:create(editBoxSize, "images/ui/login/1.png");
    editCode:setAnchorPoint(cc.p(0, 0.5));
    editCode:setPosition(codeInput:getPosition());
    editCode:setFontName(getLocFontName());
    editCode:setFontSize(TextStyleM.TEXT_SIZE_SMALL2);
    editCode:setPlaceHolder(getLocStr("please_input_activate_code"));
    editCode:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_GRAY2);
    editCode:setFontColor(TextStyleM.TEXT_OUTLINE_COLOR_DARK);
    editCode:setMaxLength(32);
    editCode:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
    self.editCode = editCode;
    codeInput:getParent():addChild(editCode);
    codeInput:setVisible(false);
end







